package com.cloudwalk.libscan.decoding;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.nfc.FormatException;
import android.os.Message;
import android.text.TextUtils;

import com.cloudwalk.libscan.constant.ScanConstants;
import com.cloudwalk.libscan.manager.CloudScanManager;
import com.cloudwalk.libscan.utils.BitmapUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

import java.util.Hashtable;
import java.util.Vector;

/**
 * @ProjectName: ScanDemo
 * @Package: com.yuncon.structured
 * @ClassName: CloudScan
 * @Description: 对外提供扫码的调用
 * @Author: YCKJ1775
 * @CreateDate: 2019/8/12 15:09
 * @UpdateUser: YCKJ1775
 * @UpdateDate: 2019/8/12 15:09
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class CloudScan {
    /**
     * 初始化实例
     */
    private static volatile CloudScan instance = null;
    /**
     * 回调接口
     */
    private ScanCallback mScanCallback;
    /**
     * 扫码状态值
     */
    private int mScanState;
    /**
     * 解析线程
     */
    private DecodeThread decodeThread;
    /**
     * 暂停扫码
     */
    private volatile boolean isStopScan = false;
    /**
     * 线程池
     */

    /**
     * 设置当前扫码状态
     *
     * @return
     */
    protected void setScanState(int scanState) {
        this.mScanState = scanState;
    }

    /**
     * 扫码回调接口
     */
    public interface ScanCallback {
        /**
         * 返回扫码结果
         *
         * @param str 扫码结果
         */
        void onScanResult(String str);
    }

    /**
     * 私有化构造方法
     */
    private CloudScan() {
    }

    /**
     * 以单例模式获取实例化对象
     *
     * @return
     */
    public static CloudScan getInstance() {
        if (instance == null) {
            synchronized (CloudScan.class) {
                if (instance == null) {
                    instance = new CloudScan();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化扫码库
     */
    public void initScanner() {
        decodeThread = new DecodeThread();
        decodeThread.start();
    }

    /**
     * 开始扫码
     */
    public void startScan() {
        isStopScan = false;
        mScanState = ScanConstants.RESTART_PREVIEW;
    }

    /**
     * 停止扫码
     *
     * @return
     */
    public void stopScan() {
        isStopScan = true;
    }

    /**
     * 释放扫码库资源
     */
    public void releaseScanner() {
        if (decodeThread != null && decodeThread.getHandler() != null) {
            quitSynchronously();
        }
    }

    /**
     * 同步退出扫码库
     */
    private void quitSynchronously() {
        Message quit = Message.obtain(decodeThread.getHandler(), ScanConstants.QUIT);
        if (decodeThread.isAlive()) {
            quit.sendToTarget();
        }
        try {
            decodeThread.join();
        } catch (InterruptedException e) {
            // continue
        }
        // Be absolutely sure we don't send any queued up messages
        decodeThread.getHandler().removeMessages(ScanConstants.DECODE_SUCCEEDED);
        decodeThread.getHandler().removeMessages(ScanConstants.DECODE_FAILED);
    }


    /**
     * 设置扫码结果回调
     *
     * @param callback
     */
    public void setScanResultListener(ScanCallback callback) {
        this.mScanCallback = callback;
    }

    /**
     * 处理解码器返回扫码结果
     *
     * @param result
     */
    protected void handleDecode(Result result) {
        if (result == null) {
            mScanState = ScanConstants.DECODE_FAILED;
            mScanCallback.onScanResult("识别失败");
        } else {
            mScanState = ScanConstants.DECODE_SUCCEEDED;
            String resultString = result.getText();
            if (TextUtils.isEmpty(resultString)) {
                mScanCallback.onScanResult("");
            } else {
                mScanCallback.onScanResult(resultString);
            }
        }
    }

    /**
     * 送数据帧到扫码库进行解析
     *
     * @param data
     */
    public void pushFrame(byte[] data, int width, int height) {
        if (isStopScan) {
            return;
        }
        if (mScanState == ScanConstants.RESTART_PREVIEW
                || mScanState == ScanConstants.DECODE_FAILED) {
            mScanState = ScanConstants.DECODE;
            if (decodeThread.getHandler() != null) {
                Message message = decodeThread.getHandler().obtainMessage(ScanConstants.DECODE, width,
                        height, data);
                message.sendToTarget();
            }
        }
    }

    /**
     * 送图片到扫码库进行解析
     *
     * @param bitmap
     */
    public void putAlbumPic(Bitmap bitmap) {
        if (isStopScan) {
            return;
        }
        if (mScanState == ScanConstants.RESTART_PREVIEW
                || mScanState == ScanConstants.DECODE_FAILED) {
            mScanState = ScanConstants.PIC_DECODE;
            if (decodeThread.getHandler() != null) {
                Message message = decodeThread.getHandler().obtainMessage(ScanConstants.PIC_DECODE,
                        bitmap);
                message.sendToTarget();
            }
        }
    }
}
